package com.zxs.domain.handel.subject;

import com.zxs.common.entity.Result;
import com.zxs.common.enums.SubjectInfoTypeEnum;
import com.zxs.domain.entity.dto.SubjectInfoDTO;
import com.zxs.infra.basic.entity.SubjectBrief;
import com.zxs.infra.basic.entity.SubjectJudge;
import com.zxs.infra.basic.mapper.SubjectMappingMapper;
import com.zxs.infra.basic.service.impl.SubjectBriefServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 简答的题目的策略类
 * @author zxs
 * @date 2025/11/16 上午4:07
 */
@Component
public class BriefTypeHandel implements SubjectTypeHandel{

    @Autowired
    private SubjectBriefServiceImpl subjectBriefService;

    @Autowired
    private SubjectMappingMapper subjectMappingMapper;

    @Override
    public SubjectInfoTypeEnum getHandelType() {
        return SubjectInfoTypeEnum.BRIEF;
    }

    @Override
    public Result add(SubjectInfoDTO subjectInfoDTO) {
        //简答题目的插入

        Result<Object> result = new Result<>();
        List<SubjectBrief> subjectBriefs = new ArrayList<>();
        //单选题目的插入
        if (CollectionUtils.isEmpty(subjectInfoDTO.getSubjectAnswerDTOS())) {
            result = Result.failed("选项不能为空！");
            return result;
        }
        subjectInfoDTO.getSubjectAnswerDTOS().forEach(option -> {
            SubjectBrief subjectBrief = new SubjectBrief();
            BeanUtils.copyProperties(option, subjectBrief);
            subjectBrief.setSubjectId(subjectInfoDTO.getId());
            subjectBriefs.add(subjectBrief);
        });

        Integer i = subjectBriefService.batchInsert(subjectBriefs);
        if (i == subjectBriefs.size()) {
            result =Result.success("插入简答题成功");
            return result;
        }
        return result;
    }

    @Override
    public SubjectInfoDTO query(int subjectId) {
        SubjectInfoDTO subjectInfoDTO = new SubjectInfoDTO();
        //查找正确答案
        SubjectBrief subjectBrief = subjectBriefService.queryBySubjectId(subjectId);

        subjectInfoDTO.setSubjectAnswer(subjectBrief.getSubjectAnswer());

        List<String> labelNames = subjectMappingMapper.selectLabelNames(subjectId);
        subjectInfoDTO.setLabelNames(labelNames);
        return subjectInfoDTO;
    }
}